package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.mapper.UserMapper;
import com.example.demo.entity.Message;
import com.example.demo.entity.User;
import com.example.demo.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

@Controller
public class GetInfoController {
    @Autowired
    private UserMapper ums;
    @RequestMapping("/getInfo")
    public void getInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User u=(User) request.getSession().getAttribute("user");
        if(u==null)  response.getWriter().write(JSONObject.toJSONString(Message.failure("error")));
        else {
            UserInfo userinfo=ums.selectByNames(u.getName());
            HashMap<String, String> Sites = new HashMap<String, String>();
            Sites.put("name",u.getName());
            Sites.put("email",userinfo.getEmail());
            Sites.put("age",userinfo.getAge().toString());
            Sites.put("sex",userinfo.getSex().toString());
            Sites.put("nation",userinfo.getNation());
            response.getWriter().write(JSONObject.toJSONString(Message.success(Sites)));
        }
    }
}
